package com.jk.kdd.system.controller;

import com.jk.kdd.base.BaseController;
import com.jk.kdd.system.model.SystemUser;
import com.jk.kdd.system.service.SystemUserService;
import com.jk.kdd.system.vo.SystemUserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.framework.util.CastUtil;
import org.smart4j.framework.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import util.PagedResult;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by DELL on 2016/10/9.
 */
@Controller
@RequestMapping("/main")
public class HomePageController extends BaseController{

    private static final Logger LOGGER = LoggerFactory.getLogger(HomePageController.class);

    @Resource(name = "systemUserService")
    private SystemUserService systemUserService;


    /**
     *  首页画面
     */
    @RequestMapping(value="/homePage",method= RequestMethod.GET)
    public String homePage(HttpServletRequest request, Model model) {
        return "main/home_page2";
    }

    /**
     *  设置广告主
     */
    @RequestMapping(value="/setAdUser",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> setAdUser() {

        String companyUserId = getRequestStringValue  ("companyUserId");

        if (StringUtil.isNotEmpty(companyUserId)) {
            setSessionValue("companyUserId",companyUserId);
        }

        HashMap<String,Object> data = new HashMap<String,Object>();
        HashMap<String,Object> param = new HashMap<String,Object>();
        param.put("code",1);
        param.put("msg","操作成功！");
        param.put("data",data);

        return param;
    }


    /**
     *  取得管理的广告主数据
     */
    @RequestMapping(value="/manageAdData",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getManageAdInfo() {

        String operateId = getSessionStringValue("operateId");
        String companyUserId = getSessionStringValue("companyUserId");

        // 当前广告主信息
        SystemUser systemUser = new SystemUser();
        systemUser.setId(CastUtil.castLong(companyUserId));
        SystemUserVO systemUserVO = systemUserService.getSystemUserByPK(systemUser);

        // 管理的广告主用户
        List<SystemUserVO> manageAdUserList = systemUserService.getManageAdSystemUser(operateId);

        // 当前选择的广告主Map
        Map<String,Object> currentUserMap = new HashMap<>();
        currentUserMap.put("id",systemUserVO.getId());
        currentUserMap.put("email",systemUserVO.getEmail());
        currentUserMap.put("telephone",systemUserVO.getTelephone());
        currentUserMap.put("companyName",systemUserVO.getCompanyName());

        // 管理的广告主Map集合
        List<Map<String,Object>> userMapList = new ArrayList<>();
        for (int i = 0; i < manageAdUserList.size(); i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",manageAdUserList.get(i).getId());
            map.put("email",manageAdUserList.get(i).getEmail());
            map.put("telephone",manageAdUserList.get(i).getTelephone());
            map.put("companyName",manageAdUserList.get(i).getCompanyName());
            userMapList.add(map);
        }

        Map<String,Object> data = new HashMap<String,Object>();
        data.put("currentAdUser",currentUserMap);
        data.put("manageAdUserList",userMapList);
        HashMap<String,Object> param = new HashMap<String,Object>();
        param.put("code",1);
        param.put("msg","操作成功！");
        param.put("data",data);

        return param;
    }

    /**
     *  取得首页数据
     */
    @RequestMapping(value="/viewData",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> viewData() {

        String companyUserId = getSessionStringValue("companyUserId");
        String pageNum = getRequestStringValue("pageNum");
        if(pageNum == null || pageNum.equals("")){
            pageNum = "1";
        }
        String pageSize = getRequestStringValue("pageSize");
        if(pageSize == null || pageSize.equals("")){
            pageSize = "0";
        }
        // 当前广告主信息
        SystemUser systemUser = new SystemUser();
        systemUser.setId(CastUtil.castLong(companyUserId));
        SystemUserVO systemUserVO = systemUserService.getSystemUserByPK(systemUser);

        HashMap<String,Object> data = new HashMap<>();
        data.put("currentAdUser",systemUserVO);

        HashMap<String,Object> param = new HashMap<String,Object>();
        param.put("code",1);
        param.put("msg","操作成功！");
        param.put("data",data);

        return param;
    }


}
